package com.ym521.emojipack.keyboard.adapter.viewHolder;

import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ym521.emojipack.R;
import com.ym521.emojipack.emoji.EmojiPackBean;
import com.ym521.emojipack.keyboard.EmoiiPackClickListener;
import com.ym521.emojipack.keyboard.adapter.EmojiPackItemAdapter;
import com.ym521.emojipack.util.Utils;

import java.util.Arrays;

/**
 * @author Ym
 * E-mail: 2435970206@qq.com
 * createTime:2023/9/24
 * explain:
 */
public class EmojiPackGroupViewHolder extends RecyclerView.ViewHolder {
    private TextView tvTitle;
    private RecyclerView rlvEmojiPack;

    public EmojiPackGroupViewHolder(@NonNull View itemView) {
        super(itemView);
        tvTitle = itemView.findViewById(R.id.tvTitle);
        rlvEmojiPack = itemView.findViewById(R.id.rlvEmojiPack);
    }

    public void showView(EmojiPackBean itemData, boolean useSystemDefault, EmoiiPackClickListener listener) {
        ViewGroup.LayoutParams layoutParams = rlvEmojiPack.getLayoutParams();
        int ext = itemData.getData().length % 8 > 0 ? 1 : 0;
        int line = (itemData.getData().length / 8) + ext;
        int itemHeight = Utils.dip2px(rlvEmojiPack, 40f);
        layoutParams.height = itemHeight * line;
        rlvEmojiPack.setLayoutParams(layoutParams);
        tvTitle.setVisibility(itemData.getTitle().isEmpty() ? View.GONE : View.VISIBLE);
        tvTitle.setText(itemData.getTitle());
        EmojiPackItemAdapter emojiPackItemAdapter = new EmojiPackItemAdapter();
        emojiPackItemAdapter.listener = listener;
        rlvEmojiPack.setLayoutManager(new GridLayoutManager(rlvEmojiPack.getContext(), 8));
        rlvEmojiPack.setAdapter(emojiPackItemAdapter);
        rlvEmojiPack.setItemAnimator(null);
        emojiPackItemAdapter.addAllData(Arrays.asList(itemData.getData()), useSystemDefault);
    }
}
